﻿//
// Copyright (c) 2024 BPer
//

#include <bpersdp/BPerAnswerSdp.h>
#include <bpersdp/BPerSdp.h>

char* g_http_response="HTTP/1.1 200 OK\r\nAllow: OPTIONS, GET, HEAD, POST\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: POST\r\nConnection: Close\r\nContent-Length: %d\r\nContent-Type: application/json\r\nServer: metaRTC3\r\n\r\n%s";
const char* g_http_content2="{\"code\":0,\"server\":\"metaRTC\",\"sdp\":\"%s\",\"sessionid\":\"%s\"}";
const char* g_http_content="{\"code\":0,\"id\":\"metaRTC_%d\",\"sdp\":\"%s\",\"type\":\"answer\"}";
int32_t g_bPer_sdp_answerId=0;
int32_t bPer_sdp_getAnswerSdp(BPerRtcSession* session,char* answer){
    char* sdp=NULL;
	char* sdpstr=(char*)bPer_calloc(1024*12,1);

    bPer_sdp_genLocalSdp(session,session->context.streamConfig->localPort, &sdp,session->context.streamConfig->direction);

    bPer_sprintf(sdpstr,sizeof(sdpstr),g_http_content,g_bPer_sdp_answerId++,sdp);
	bPer_sprintf(answer,sizeof(answer),g_http_response,bPer_strlen(sdpstr),sdpstr);
	bPer_free(sdp);

	bPer_free(sdpstr);
	return BPer_Ok;
}

int32_t bPer_sdp_getAnswerSdp2(BPerRtcSession* session,char* answer){
    char* sdp=NULL;//(char*)bPer_calloc(1024*12,1);
	char* sdpstr=(char*)bPer_calloc(1024*12,1);
	char* sessionid=(char*)bPer_calloc(64,1);
    bPer_sdp_genLocalSdp(session,session->context.streamConfig->localPort, &sdp,session->context.streamConfig->direction);
	bPer_sprintf(sessionid, sizeof(sessionid),"%s:%s",session->local_ufrag,session->remote_ufrag);
	bPer_sprintf(sdpstr, sizeof(sdpstr),g_http_content2,sdp,sessionid);
	bPer_sprintf(answer, sizeof(answer),g_http_response,bPer_strlen(sdpstr),sdpstr);
	bPer_free(sdp);
	bPer_free(sessionid);
	bPer_free(sdpstr);
	return BPer_Ok;
}
